package airthmetic.exercise.string;

import java.util.PriorityQueue;

public class _905_按奇偶排序数组 {

    public int[] sortArrayByParity(int[] nums) {
        if (nums == null || nums.length < 2) {
            return nums;
        }
        int left = 0;
        int right = nums.length - 1;

        while (left < right) {
            while ( left < right && (nums[left] & 1) == 0) {
                // 偶数
                left++;
            }

            while (left < right && (nums[right] & 1) == 1) {
                // 奇数
                right--;
            }
            if(left >= right){
                break;
            }
            // 走到这说明左侧不是偶数 右侧不是奇数

            swap(nums, left, right);
            left++;
            right--;

        }

        return nums;
    }

    public void swap(int[] nums, int left, int right) {
        if (left == right) {
            return;
        }
        nums[left] ^= nums[right];
        nums[right] ^= nums[left];
        nums[left] ^= nums[right];
    }


    public static void main(String[] args) {
        _905_按奇偶排序数组 v = new _905_按奇偶排序数组();
//        int[] nums = {0,1};
        int[] nums = {0,2};
        int[] ints = v.sortArrayByParity(nums);

        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }
}
